package com.hzdl.flower.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.flower.bean.Cart;
import com.hzdl.flower.bean.Flower;
import com.hzdl.flower.bean.User;
import com.hzdl.flower.bean.Wishlist;
import com.hzdl.flower.service.CartService;
import com.hzdl.flower.service.FlowerService;
import com.hzdl.flower.service.WishListService;
import com.hzdl.flower.util.HzdlJsonAjax;
import com.hzdl.flower.vo.CartVo;
import com.hzdl.flower.vo.FlowerVo;
import com.hzdl.flower.vo.WishlistVo;

import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("flower")
public class ShopController {
    @Autowired
    private FlowerService flowerService;

    @Autowired
    private WishListService wishListService;

    @Autowired
    private CartService CartService;

    @RequestMapping("indexInit")
    public String indexInit(Model model, HttpSession session) {


        User user = (User) session.getAttribute("user");
        if (user != null) {
            model.addAttribute("user", user);
        }

        session.getAttribute("caList");
        session.getAttribute("flList");

        List<Flower> flowers = flowerService.findFlower(null);
        List<Flower> newflowers = flowerService.findNew(null);
        List<Flower> hotflowers = flowerService.findhot(null);

        model.addAttribute("fList", flowers);
        model.addAttribute("newfList", newflowers);
        model.addAttribute("hotfList", hotflowers);


        if (user != null) {
            //购物车数据

            CartVo cartVo = new CartVo();
            cartVo.setUserid(user.getId());
            FlowerVo flowerVo = new FlowerVo();
            List<Cart> caList = CartService.findByUid(cartVo);
            List<Flower> flList = new ArrayList<>();
            BigDecimal sum = new BigDecimal(0);
            for (Cart cart : caList) {
                flowerVo.setId(cart.getFid());
                Flower flower = flowerService.findById(flowerVo);
                flList.add(flower);
                Integer count = cart.getCount();
                BigDecimal count1 = new BigDecimal(count.toString());
                BigDecimal fprice = cart.getFprice();
                BigDecimal total = fprice.multiply(count1);
                sum = sum.add(total);
            }
            model.addAttribute("caList", caList);
            model.addAttribute("flList", flList);
            model.addAttribute("sum", sum);

        }

        return "shop/index";

    }

    @RequestMapping("productInit")
    public String productInit(Model model, FlowerVo flowerVo, Integer id, Integer type, String color, HttpSession session) {

        List<Flower> list = flowerService.findByTypeAndColor(type, color);
        model.addAttribute("sList", list);

        if (id == null) {
            id = 1;
        }

        flowerVo.setId(id);
        Flower flower = flowerService.findById(flowerVo);
        model.addAttribute("flower", flower);


        //购物车数据
        User user = (User) session.getAttribute("user");
        if (user != null) {
            CartVo cartVo = new CartVo();
            cartVo.setUserid(user.getId());
            FlowerVo flowerVo1 = new FlowerVo();
            List<Cart> caList = CartService.findByUid(cartVo);
            List<Flower> flList = new ArrayList<>();
            BigDecimal sum = new BigDecimal(0);
            for (Cart cart : caList) {
                flowerVo.setId(cart.getFid());
                Flower flower1 = flowerService.findById(flowerVo);
                flList.add(flower1);
                Integer count = cart.getCount();
                BigDecimal count1 = new BigDecimal(count.toString());
                BigDecimal fprice = cart.getFprice();
                BigDecimal total = fprice.multiply(count1);
                sum = sum.add(total);
            }

            model.addAttribute("sum", sum);
            model.addAttribute("caList", caList);
            model.addAttribute("flList", flList);
        }
        return "shop/product";
    }

    @RequestMapping("shopInit")
    public String shopInit(Model model, Integer pageNum, FlowerVo flowerVo, BigDecimal min, BigDecimal max, HttpSession session) {
        Map<String, Object> map = new HashMap<>();

        map.put("fname", flowerVo.getFname());
        map.put("color", flowerVo.getColor());
        map.put("type", flowerVo.getType());

        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo pageInfo = flowerService.findPage(map, pageNum, 9);
        model.addAttribute("pageInfo", pageInfo);

        //购物车数据
        User user = (User) session.getAttribute("user");
        if (user != null) {
            CartVo cartVo = new CartVo();
            cartVo.setUserid(user.getId());
            FlowerVo flowerVo1 = new FlowerVo();
            List<Cart> caList = CartService.findByUid(cartVo);
            List<Flower> flList = new ArrayList<>();
            BigDecimal sum = new BigDecimal(0);

            for (Cart cart : caList) {
                flowerVo.setId(cart.getFid());
                Flower flower1 = flowerService.findById(flowerVo);
                flList.add(flower1);
                Integer count = cart.getCount();
                BigDecimal count1 = new BigDecimal(count.toString());
                BigDecimal fprice = cart.getFprice();
                BigDecimal total = fprice.multiply(count1);
                sum = sum.add(total);
            }

            model.addAttribute("caList", caList);
            model.addAttribute("flList", flList);
            model.addAttribute("sum", sum);

        }
        return "shop/shop";
    }
    @RequestMapping("wishListInit")
    public String wishListInit(Model model, WishlistVo wishlistVo, HttpSession session) {

        User user = (User) session.getAttribute("user");

        if (user == null) {
            model.addAttribute("msg", "请先登录哦");
        }else {
            List<Flower> fList = new ArrayList<>();
            wishlistVo.setUid(user.getId());
            List<Wishlist> fids = wishListService.findFids(wishlistVo);
            for (Wishlist wishlist : fids) {
                FlowerVo flowerVo = new FlowerVo();
                flowerVo.setId(wishlist.getFid());
                Flower byId = flowerService.findById(flowerVo);
                fList.add(byId);
            }

            if (fids == null) {
                model.addAttribute("msg", "还没有心仪花花哦");
            }
            model.addAttribute("fList", fList);
        }
        return "shop/wishlist";
    }

    @ResponseBody
    @RequestMapping("addwish")
    public HzdlJsonAjax addwish(FlowerVo flowerVo, HttpSession session, Integer id) {

        User user = (User) session.getAttribute("user");

        if (user == null) {
            return new HzdlJsonAjax("请先登录哦", 0);
        }
        WishlistVo wishlistVo = new WishlistVo();

        wishlistVo.setFid(id);
        wishlistVo.setUid(user.getId());
        Wishlist wishlist = wishListService.cheWish(wishlistVo);
//        String msg, Integer code, Object data
        if (wishlist != null) {
            return new HzdlJsonAjax("已经在您的心愿单了哦", 0);
        }
        int row = wishListService.addWish(wishlistVo);
        if (row != 0) {
            return new HzdlJsonAjax("收藏成功", 0);
        }
        return new HzdlJsonAjax("收藏失败", 0);
    }
    @ResponseBody
    @RequestMapping("delwish")
    public HzdlJsonAjax delwish(WishlistVo wishlistVo, Integer id) {

      wishlistVo.setFid(id);

        int i = wishListService.delWish(wishlistVo);

        if (i != 0) {
            return new HzdlJsonAjax("删除成功", 0);
        }
        return new HzdlJsonAjax("删除失败", 0);
    }



    @ResponseBody
    @RequestMapping("addCart")
    private HzdlJsonAjax addCart(Model model, Integer fid, Integer count, HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("user", user);
        if (user != null) {
            CartVo cartVo = new CartVo();
            cartVo.setFid(fid);
            cartVo.setCount(count);
            cartVo.setUserid(user.getId());
            int row = CartService.upCart(cartVo);
if (row!=0){
    return new HzdlJsonAjax("添加成功", 1);
}else {
    return new HzdlJsonAjax("添加失败", 0);
}

//        } else {
//
//        }
//重定向
//        return "redirect:/flower/shopInit";
        }
        return new HzdlJsonAjax("请先登录", 0);
    }
//
//    @ResponseBody
//    @RequestMapping("cartInit")
//    private HzdlJsonAjax cartInit(CartVo cartVo, Model model) {
//        FlowerVo flowerVo = new FlowerVo();
//        List<Cart> caList = CartService.findByUid(cartVo);
//        List<Flower> flList = new ArrayList<>();
//        for (Cart cart : caList) {
//            flowerVo.setId(cart.getFid());
//            Flower flower = flowerService.findById(flowerVo);
//            flList.add(flower);
//        }
//        model.addAttribute("caList",caList);
//        model.addAttribute("flList",flList);
//        return null;
//    }


}